﻿<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

<script type="text/C#" runat="server">
    string proxyURL = string.Empty;
    string soapAction = string.Empty;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"].ToString());
        }
        catch
        {
            proxyURL = HttpUtility.UrlDecode(Request.Form["u"]);
            try
            {
                soapAction = HttpUtility.UrlDecode(Request.Form["soapAction"]);
            }
            catch { }
        }
        finally
        {
            try
            {
                if (proxyURL != string.Empty)
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL);
                    request.Method = Request.RequestType.ToUpper();
                    if (request.Method == "POST")
                    {
                        System.Net.ServicePointManager.Expect100Continue = false;
                        if (Request.Form["postData"].StartsWith("<?xml"))
                        {
                            XmlDocument soapEnvelopeXml = new XmlDocument();
                            soapEnvelopeXml.LoadXml(HttpUtility.UrlDecode(Request.Form["postData"]));
                            HttpWebRequest webRequest = CreateWebRequest(proxyURL, soapAction);
                            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

                            // begin async call to web request.
                            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                            // suspend this thread until call is complete.
                            asyncResult.AsyncWaitHandle.WaitOne();

                            // get the response from the completed web request.
                            string soapResult;
                            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                            {
                                soapResult = rd.ReadToEnd();
                                Response.ContentType = webResponse.ContentType;
                                Response.Write(soapResult);
                            }
                        }
                        else
                        {
                            Response.ContentType = "text/xml";
                            Response.Write(HttpUrlencodedPost(proxyURL, Request.Form["postData"]));
                        }                       
                    }
                    else
                    {
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        string responseContentType = response.ContentType;
                        Stream content = response.GetResponseStream();
                        StreamReader contentReader = new StreamReader(content);
                        Response.ContentType = responseContentType;
                        Response.Write(contentReader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                throw new WebException(ex.Message);
            }
        }
    }

    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

    private static string HttpUrlencodedPost(string URI, string Parameters)
    {
        System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
        //Add these, as we're doing a POST
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(HttpUtility.UrlDecode(Parameters));
        req.ContentLength = bytes.Length;
        System.IO.Stream os = req.GetRequestStream();
        os.Write(bytes, 0, bytes.Length); //Push it out there
        os.Close();
        System.Net.WebResponse resp = req.GetResponse();
        if (resp == null) return null;
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        return sr.ReadToEnd().Trim();
    }
</script>